package com.msc.cili.common.utils;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;


/**
 * IP 工具类
 *
 */
public class IPUtil {

    /**
     * 未知IP
     */
    private static final String IP_UNKNOWN = "unknown";

    /**
     * 获取IP地址
     *
     * @param request
     * @return
     */
    public static String getIpAddr(HttpServletRequest request) {
        if (request == null) {
            return null;
        }
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || IP_UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || IP_UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || IP_UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }

        // 取X-Forwarded-For中第一个非unknown的有效IP字符串。
        if (ip.indexOf(",") != -1) {
            String[] ipList = ip.split(",");
            String tmp;
            for (int i = 0; i < ipList.length; i++) {
                tmp = ipList[i];
                if (tmp != null && !IP_UNKNOWN.equalsIgnoreCase(tmp.trim())) {
                    return tmp.trim();
                }
            }
        }
        return ip;
    }

    /**
     * 获得本机的机器名称
     *
     * @return
     */
    public static String getLocalHostName() {
        try {
            InetAddress addr = InetAddress.getLocalHost();
            return addr.getHostName();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 返回主机名的全限定域名
     *
     * @return
     */
    public static String getFullyLocalHostName() {
        String hostName = null;
        try {
            InetAddress inet = InetAddress.getLocalHost();
            hostName = inet.getCanonicalHostName();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return hostName;
    }


    /**
     * 返回本机IP
     *
     * @return
     */
    public static String getLocalHostAddress() {
        try {
            InetAddress inet = InetAddress.getLocalHost();
            String hostAddress = inet.getHostAddress();
            return hostAddress;
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 判断本机是否和传入的域名一致
     *
     * @param domainName
     * @return
     */
    public static boolean isDomainEqualsLocal(String domainName) {
        if (StringUtils.isBlank(domainName)) {
            return false;
        }
        try {
            InetAddress host = InetAddress.getByName(domainName);
            String domainAddress = host.getHostAddress();
            if (domainAddress.equals(getLocalHostAddress())) {
                return true;
            } else {
                return false;
            }

        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return false;
    }
}
